<?php include "../../config.php"; ?>

<?

$wechat_detail=select_one_line_all_items($table_prefix."app_mp","nid > 0");

$_member=select_lines_all_items($table_prefix."wechat_member","","openid != '' and (unionid is Null or unionid = '' )","5");

//print_r2($_member);


//配置参数的数组
$CONF =  array(
'__APPID__' =>$wechat_detail['mpapp_id'],
'__SERECT__' =>$wechat_detail['mpapp_secret']
);



if($wechat_detail['refresh_token']<date("YmdHis"))
{


//echo $wechat_detail['refresh_token'];
//echo "<br/>已经超时<br/>";
//echo date("YmdHis");


//过期了就要重新拿,没过期就直接提取可以了。
$gettokenUrl  =  "https://api.weixin.qq.com/cgi-bin/token".
"?grant_type=client_credential".
"&appid=" . $CONF['__APPID__'].
"&secret=".$CONF['__SERECT__']
;


$get_json=file_get_contents($gettokenUrl);


$access_token=substr($get_json,17,136);
$token['ai_access_token']=substr($get_json,17,136);


$token_expire=date("YmdHis",strtotime('+1 hour'));
$token['ai_refresh_token']=date("YmdHis",strtotime('+1 hour'));



    $update_token=update_date($table_prefix."app_mp",$token,"nid",'1');
    $query = do_query($update_token) or die (mysqli_error().$update_token);
    mysqli_close($con);
}


else
{
    $access_token=$wechat_detail['access_token'];
}



foreach ($_member as $k=>$v)
{
//从接口获取用户的信息
$_getcontent=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".
                               $_member[$k]['openid']);
    $_contents=json_decode($_getcontent,true);

    print_r2($_contents);


    $_new_detail['ai_unionid']         = $_contents['unionid'];
    $_new_detail['ai_subscribe_time']  = $_contents['subscribe_time'];
    $_new_detail['ai_subscribe_scene'] = $_contents['subscribe_scene'];

    echo $_sql=update_date($table_prefix."wechat_member",$_new_detail,"openid",$_member[$k]['openid']);
    $result = do_query($_sql);
    mysqli_close($con);

}







?> 


 